package com.lanou.myim.model;

import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.ImageMessageBody;
import com.easemob.chat.TextMessageBody;
import com.easemob.chat.VoiceMessageBody;
import com.lanou.myim.application.BaseApplication;
import com.lanou.myim.entity.EventBusConstant;
import com.lanou.myim.presenter.OnGetMsgsCompletedListener;

import java.io.File;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/3/15.
 */
public class PrivateChatModel implements PrivateChatModelInterface{

    @Override
    public void getMsgs(String userName,OnGetMsgsCompletedListener listener) {
        EMConversation emConversation = EMChatManager.getInstance().getConversation(userName);
        List<EMMessage> messages = emConversation.getAllMessages();
        listener.onCompleted(messages);
    }

    @Override
    public void getMoreMsgs(String userName, OnGetMsgsCompletedListener listener) {
        EMConversation emConversation = EMChatManager.getInstance().getConversation(userName);
        List<EMMessage> messages = emConversation.getAllMessages();

        List<EMMessage>messagesMore = emConversation.loadMoreMsgFromDB(messages.get(0).getMsgId(), 5);
        int count = messagesMore.size();
        if (count == 0){
            Toast.makeText(BaseApplication.applicationContext, "聊天记录已全部加载完毕", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(BaseApplication.applicationContext, "加载了"+ count + "条数据", Toast.LENGTH_SHORT).show();
        }
        listener.onCompleted(messages);
    }

    @Override
    public void clearUnreadMsgs(String userName) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(userName);
        conversation.markAllMessagesAsRead();
        EventBus.getDefault().post(EventBusConstant.REFRESH_UNREAD_COUNT);
    }


    @Override
    public void clearMsgs(String userName,OnGetMsgsCompletedListener listener) {
        EMChatManager.getInstance().clearConversation(userName);
        List<EMMessage> messages = EMChatManager.getInstance().getConversation(userName).getAllMessages();
        listener.onCompleted(messages);
    }

    @Override
    public void sendText(String userName,String  content,boolean isGroupChat,EMCallBack emCallBack) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(userName);
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        if (isGroupChat){
            message.setChatType(EMMessage.ChatType.GroupChat);
        }
        TextMessageBody textMessageBody = new TextMessageBody(content);
        message.addBody(textMessageBody);
        message.setReceipt(userName);
        message.setMsgTime(System.currentTimeMillis());
        conversation.addMessage(message);
        EMChatManager.getInstance().sendMessage(message, new EMCallBack() {
            @Override
            public void onSuccess() {

            }

            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }

    @Override
    public void sendPicture(String username,String filePath,boolean isGroupChat,EMCallBack emCallBack) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(username);
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
        if (isGroupChat){
            message.setChatType(EMMessage.ChatType.GroupChat);
        }
        ImageMessageBody body = new ImageMessageBody(new File(filePath));
        // body.setSendOriginalImage(true);
        message.addBody(body);
        message.setReceipt(username);
        message.setMsgTime(System.currentTimeMillis());
        conversation.addMessage(message);
        EMChatManager.getInstance().sendMessage(message, emCallBack);
    }

    @Override
    public void sendVoice(String userName, String filePath,int len,boolean isGroupChat,EMCallBack emCallBack) {
        EMConversation conversation = EMChatManager.getInstance().getConversation(userName);
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.VOICE);
        if (isGroupChat){
            message.setChatType(EMMessage.ChatType.GroupChat);
        }
        VoiceMessageBody body = new VoiceMessageBody(new File(filePath),len);
        message.addBody(body);
        message.setReceipt(userName);
        conversation.addMessage(message);
        EMChatManager.getInstance().sendMessage(message, emCallBack);
    }


}
